package com.home.free.volatileTest;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReentrantLock;

public class JmmVolatile {
    private static int  total = 0;

    private static Object object = new Object();

    private static ReentrantLock reentrantLock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(1);

        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                try {
                    countDownLatch.await();

                    for (int j = 0; j < 1000; j++) {
                        reentrantLock.lock();
//                        synchronized (object) {
                            total++;
//                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }

        Thread.sleep(500);

        countDownLatch.countDown();

        Thread.sleep(2000);

        System.out.println(total);
    }

    public void readAndWrite() {

    }
}
